package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.result.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        String jsonString = JSONObject.toJSONString(stringObjectMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        String depcode = department.getDepcode();
        if (depcode.isEmpty()) {
            throw new YyghException(20001, "depcode为空经检查输入内容");
        }
        String hoscode = department.getHoscode();

        if (hoscode.isEmpty()) {
            throw new YyghException(20001, "hoscode为空经检查输入内容");
        }
        Department department1 = departmentRepository.findDepartmentByHoscodeAndDepcode(hoscode, depcode);


        if (department1 == null) {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        } else {
            department.setId(department1.getId());
            department.setUpdateTime(new Date());
            department.setCreateTime(department1.getCreateTime());
            departmentRepository.save(department);
        }
    }

    @Override
    public Result getDepartmentList(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> objectMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = objectMap.get("hoscode").toString();
        if (hoscode.isEmpty()){
            throw new YyghException(20001,"hoscode为空");
        }
        String pageNum = objectMap.get("page").toString();
        String pageSize = objectMap.get("limit").toString();

        Department department = new Department();
        department.setHoscode(hoscode);
        ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreCase(true).withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Example<Department> example = Example.of(department,matcher);

        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable = PageRequest.of(Integer.parseInt(pageNum),Integer.parseInt(pageSize),sort);
        Page<Department> departments = departmentRepository.findAll(example, pageable);

        return Result.ok(departments);
    }

    @Override
    public  List<DepartmentVo> getDeptByHoscode(String hoscode) {

        List<Department> departmentList = departmentRepository.findDepartmentByHoscode(hoscode);

        Map<String, List<Department>> listMap = departmentList.stream().collect(
                Collectors.groupingBy(Department::getBigcode)
        );

        List<DepartmentVo>  departmentVoList = new ArrayList<>();

        Set<String> keySet = listMap.keySet();
        keySet.forEach(key->{

            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(key);
            departmentVo.setDepname(listMap.get(key).get(0).getBigname());

            List<Department> departments = listMap.get(key);
            ArrayList<DepartmentVo> departmentVos = new ArrayList<>();

            departments.forEach(department -> {

                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                departmentVo1.setChildren(null);

                departmentVos.add(departmentVo1);

            });

            departmentVo.setChildren(departmentVos);

            departmentVoList.add(departmentVo);

        });

        return departmentVoList;
    }
}
